package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import java.util.Map;
import java.util.Objects;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl.AbstractCommandHandler;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ErrorCode;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpCommandDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpCommandResultDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpSetParticipantsDto;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.threadlocal.CommonOperateLogHolder;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.command.SetParticipantsCmd;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.event.CreateParticipantEvent;
import com.iwhalecloud.citybrain.flow.platform.manager.application.utils.SpringUtils;
import org.flowable.engine.ManagementService;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author cheng.huaxing
 * @date 2019-03-19
 */
@Service
public class SetParticipantsCommandHandler extends AbstractCommandHandler {

    @Autowired
    private ManagementService managementService;

    @Override
    public String handleType() {
        return SET_PARTICIPANTS;
    }

    @Override
    public HttpCommandResultDto handle(String params) {
        HttpSetParticipantsDto setParticipantsDto = JSONObject.parseObject(params, HttpSetParticipantsDto.class);
        return handle(setParticipantsDto);
    }

    @Override
    public HttpCommandResultDto handle(HttpCommandDto commandDto) {
        HttpSetParticipantsDto setParticipantsDto = (HttpSetParticipantsDto) commandDto;
        CommonOperateLogHolder.getContent().setRequest(setParticipantsDto);
        ProcessInstance processInstance = managementService.executeCommand(new SetParticipantsCmd(setParticipantsDto));
        publishEvent(setParticipantsDto);
        return resolveResult(processInstance, ErrorCode.SET_PARTICIPANTS_FAILED.errMessage(), SET_PARTICIPANTS_SUCCESS);
    }

    private void publishEvent(HttpSetParticipantsDto setParticipantsDto) {
        if (setParticipantsDto.getTacheList() != null && !setParticipantsDto.getTacheList().isEmpty()) {
            for (Map.Entry<String, Object> entry : setParticipantsDto.getTacheList().entrySet()) {
                JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(entry.getValue()));
                JSONArray jsonArray = jsonObject.getJSONArray("PARTICIPANTS");
                if (Objects.nonNull(jsonArray)) {
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject participant = jsonArray.getJSONObject(i);
                        CreateParticipantEvent.CreateParticipantVo createParticipantVo = new CreateParticipantEvent.CreateParticipantVo();
                        createParticipantVo.setParticipantId(participant.getString("participantId"));
                        createParticipantVo.setParticipantName(participant.getString("participantName"));
                        createParticipantVo.setParticipantType(participant.getString("participantType"));
                        createParticipantVo.setFrom(setParticipantsDto.getFrom());
                        SpringUtils.publishEvent(new CreateParticipantEvent(createParticipantVo));
                    }
                }
            }
        }
    }
}
